﻿using System;
using System.Collections.Generic;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;

namespace Pcme.Info.MvcMusicStore.Helpers
{
    public class NHibernateHelper
    {
        public static ISessionFactory GetCurrentFactory()
        {
            if (sessionFactory == null)
            {
                sessionFactory = CreateSessionFactory();
            }
            return sessionFactory;
        }

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2005
                .ConnectionString(c => c.FromConnectionStringWithKey("constr"))
                .ShowSql())
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Pcme.Info.MvcMusicStore.Models.Genre>())
                .BuildSessionFactory();
        }

        public static ISession GetSession()
        {
            return GetCurrentFactory().OpenSession();
        }

        private static ISessionFactory sessionFactory { get; set; }

    }
}
